﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 纺织贸易管理系统;

namespace BLL
{
  public static   class 可发卷BLL
    {
        public static void 卷出库(List<JuanHaoTable> juanHaos ,string danhao)
        {
            foreach (var juan in juanHaos)
            {
                juan.state = 1;
                juan.Danhao = danhao;
            }
            Connect.dbcontext.Updateable(juanHaos).ExecuteCommand();
        }
        public static void 卷出库(List<JuanHaoTable  > juanHaos )
        {
            //foreach (var j in juanHaos )
            //{
            //    var juan = JuanHaoTableService.GetOneJuanHaoTable(x => x.JuanHao == j.JuanHao );
            //    juan.state = 0;
            //    JuanHaoTableService.UpdateJuanHaoTable(juan, x => x.JuanHao == juan.JuanHao);
            //}
            juanHaos.ForEach(x => x.state = 1);
            Connect.dbcontext.Updateable(juanHaos).ExecuteCommand();         
        }
        /// <summary>
        /// 单号
        /// </summary>
        /// <param name="Danhao"></param>
        public static void 卷入库(string Danhao)
        {
            //foreach (var juan in juanhaos)
            //{ 
            //    juan.Danhao =string.Empty ;
            var juans = Connect.dbcontext.Queryable<JuanHaoTable>().Where(x => x.Danhao == Danhao).ToList();
            foreach(var juan in juans)
            {
                juan.Danhao = string.Empty;
                juan.state = 0;
            }
                Connect.dbcontext.Updateable<JuanHaoTable>(juans).ExecuteCommand();
            //}
        }
      
        /// <summary>
        /// 删除卷
        /// </summary>
        /// <param name="j">卷编号</param>
        public static string  卷删除(string  j)
        {
            var juan = JuanHaoTableService.GetOneJuanHaoTable(x => x.JuanHao == j);
            var stock = StockTableService.GetOneStockTable(x =>x.CKMC==juan.Ckmc &&x.CustomName==juan.CustomerName &&x.PM ==juan.SampleName &&x.GH==juan.GangHao&&x.YS==juan.yanse &&x.houzhengli==juan.Houzhengli &&x.RKDH ==juan.ReceiptNo );
            if (stock.PM  != string.Empty)
            {
                stock.yijianmishu -= juan.MiShu ;
                stock.yijianjuanshu -= 1;
                stock.biaoqianmishu -= juan.biaoqianmishu;
                Connect.dbcontext.Updateable(stock).ExecuteCommand (); 
            }
            Connect.dbcontext.Insertable<DeleteLog>(new DeleteLog() { date = DateTime.Now, Log = JsonConvert.SerializeObject(juan), Reason = "删除卷", useID = string.Empty }).ExecuteCommand();
            Connect.dbcontext.Deleteable(juan).ExecuteCommand();
                return "删除成功";  
        }
       public static void 卷预出库(List<JuanHaoTable> juanHaoTables,string danhao)
        {
            juanHaoTables.ForEach(x => x.Danhao = danhao);
            Connect.dbcontext.Updateable(juanHaoTables).ExecuteCommand();
        }
        
    }
}
